#include "AppStateMenu.h"
#include "AppStateManager.h"

void CAppStateMenu::OnKeyOptionsControls(SDLKey sym)
{
	//Set key to right action 
	if(KeyInput && sym != SDLK_TAB && sym != SDLK_F1 && sym != SDLK_ESCAPE)
	{
		if(choice == 0 && sym != NULL){
			LeftKey = sym;
			KeyInput = false;
		}
		else if(choice == 1 && sym != NULL){
			RightKey = sym;
			KeyInput = false;
		}
		else if(choice == 2 && sym != NULL){
			JumpKey = sym;
			KeyInput = false;
		}
		else if(choice == 3 && sym != NULL){
			SpellHotKey1 = sym;
			KeyInput = false;
		}
		else if(choice == 4 && sym != NULL){
			OffensiveSpellKey = sym;
			KeyInput = false;
		}
		else if(choice == 5 && sym != NULL){
			FlySpellKey = sym;
			KeyInput = false;
		}
		else if(choice == 6 && sym != NULL){
			SpellHotKey2 = sym;
			KeyInput = false;
		}
		else if(choice == 7 && sym != NULL){
			DefensiveSpellKey = sym;
			KeyInput = false;
		}
		else if(choice == 8 && sym != NULL){
			UtilitySpellKey = sym;
			KeyInput = false;
		}
		else if(choice == 9 && sym != NULL){
			SpellHotKey3 = sym;
			KeyInput = false;
		}
	}

	else
	{	//standard buttons.
		switch(sym) {

			case SDLK_UP:
			{
				CSoundBank::SoundControl.Play(0, 1);
				choice--;
				if(choice<0)
					choice = 10;
				break;
			}
			case SDLK_w:
			{
				CSoundBank::SoundControl.Play(0, 1);
				choice--;
				if(choice<0)
					choice = 10;
				break;
			}
			case SDLK_DOWN:
			{
				CSoundBank::SoundControl.Play(0, 1);
				this->choice++;
				if(this->choice>10)
					this->choice = 0;
				break;
			}
			case SDLK_s:
			{
				CSoundBank::SoundControl.Play(0, 1);
				this->choice++;
				if(this->choice>10)
					this->choice = 0;
				break;
			}
			case SDLK_ESCAPE:
			{
				mainChoice = OPTIONS_CHOICE;
				choice = 2;
				break;
			}

			case SDLK_RETURN:
			{
				if(choice >= 0 && choice <= 9)
					KeyInput = true;
				else if(choice == 10)
				{
					mainChoice = OPTIONS_CHOICE;
					choice = 2;
				}
			}
		}
	}
}

//Draw function for control options
void CAppStateMenu::OnRenderControls(SDL_Surface* Surf_Display)
{
	SDL_Rect Rect;
	Rect.x = 60;
	Rect.y = 150;
		
	//draw control box
	SDL_BlitSurface(ControlsBox, NULL, Surf_Display, &Rect);

	Rect.h = 10;
	Rect.w = 10;

	//draw choice dot
	if(choice < 4)
	{
		Rect.x = 60;
		Rect.y = 190 + 40*choice;
	}
	else if(choice < 7)
	{
		Rect.x = 210;
		Rect.y = 190 + 40*(choice-4);
		if (choice == 6)
			Rect.y = 190 + 40*3;
	}
	else if(choice < 10)
	{
		Rect.x = 360;
		Rect.y = 190 + 40*(choice-7);
		if (choice == 9)
			Rect.y = 190 + 40*3;
	}
	else
	{
		Rect.x = 60;
		Rect.y = 350;
	}
	SDL_FillRect(Surf_Display, &Rect, 0);

	//Draw sdlkey text
	SDL_Surface* Text_Solid;
	SDL_Colour black = {0,0,0};
	SDLKey* keys[10] = {&LeftKey, &RightKey, &JumpKey, &SpellHotKey1, &OffensiveSpellKey, &FlySpellKey, &SpellHotKey2, &DefensiveSpellKey, &UtilitySpellKey, &SpellHotKey3};
	for(int i = 0; i < 11; i++)
	{
		if(i < 4)
		{
			Rect.x = 85;
			Rect.y = 180 + 40*i;
			Text_Solid = OnRenderSDLKey(*keys[i]);
		}
		else if(i < 7)
		{
			Rect.x = 235;
			Rect.y = 180 + 40*(i-4);
			Text_Solid = OnRenderSDLKey(*keys[i]);
			if(Rect.y == 260)
				Rect.y = 300;
		}
		else if(i < 10)
		{
			Rect.x = 390;
			Rect.y = 180 + 40*(i-7);
			Text_Solid = OnRenderSDLKey(*keys[i]);
			if(Rect.y == 260)
				Rect.y = 300;
		}
		else
		{
			Rect.x = 85;
			Rect.y = 340;
			std::string text = "return";
			if(KeyInput)
				text = "enter a key input...";
				Text_Solid = TTF_RenderText_Blended(IPFont, text.c_str(), black);
		}
		SDL_BlitSurface(Text_Solid, NULL, Surf_Display, &Rect);
		SDL_FreeSurface(Text_Solid);
	}
}

//convert SDLKeys to understandable text
SDL_Surface* CAppStateMenu::OnRenderSDLKey(SDLKey input)
{
	std::string text;
	SDL_Colour black = {0,0,0};
	if(input == SDLK_SPACE)
		text = "space";
	else if(input == SDLK_RETURN)
		text = "enter";
	else if(input == SDLK_LEFT)
		text = "left";
	else if(input == SDLK_RIGHT)
		text = "right";
	else if(input == SDLK_UP)
		text = "up";
	else if(input == SDLK_DOWN)
		text = "down";
	else if(input == SDLK_PERIOD)
		text = ".";
	else if(input == SDLK_LSHIFT || input == SDLK_RSHIFT)
		text = "shift";
	else if(input == SDLK_RCTRL || input == SDLK_RCTRL)
		text = "ctrl";
	else if(input == SDLK_RALT || input == SDLK_RALT)
		text = "alt";
	else
		text = input;
		
	return TTF_RenderText_Blended(IPFont, text.c_str(), black);
}